package com.huluspace.jpahibernate.controller;

import com.huluspace.jpahibernate.dto.LoginRequest;
import com.huluspace.jpahibernate.dto.LoginResponse;
import com.huluspace.jpahibernate.dto.UserDTO;
import com.huluspace.jpahibernate.model.User;
import com.huluspace.jpahibernate.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class AuthController {

  // 注入
  private final UserService userService;
  public AuthController(UserService userService) {
    this.userService = userService;
  }

  @PostMapping("/register")
  public String register(@RequestBody User user) {
    return userService.register(user);
  }

  @PostMapping("/login")
  public LoginResponse login(@RequestBody LoginRequest request) {
    return userService.login(request.username, request.password);
  }

  @GetMapping("/users")
  public List<UserDTO> users() {
    return userService.listAllUsers();
  }
  @GetMapping("/me")
  public ResponseEntity<?> getUserInfo(@AuthenticationPrincipal UserDetails user) {
    // 或者从SecurityContextHolder获取
    return ResponseEntity.ok(Map.of(
        "username", user.getUsername(),
        "email", "无"  // 示例数据
    ));
  }
}

